#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

int	main(int argc, char **argv)
{
    printf("Hello World\n");

    regex_t reg = {0};

    char content[] = "123abc456";
    char exp[] = "[0-9]*";

    // regcomp(regex_t *restrict __preg, const char *restrict __pattern, int __cflags)
    int rc;
    rc = regcomp(&reg, exp, REG_EXTENDED | REG_NEWLINE);
    if (rc != 0) {
        char message[256];
        // regerror(int __errcode, const regex_t *restrict __preg, char *restrict __errbuf, size_t __errbuf_size)
        regerror(rc, &reg, message, 256);
        printf(">> %s\n", message);
        return 1;
    }

    size_t matchs = 2;
    regmatch_t match[matchs];
    char *match_text = content;
    
    // regexec(const regex_t *restrict __preg, const char *restrict __String, size_t __nmatch, regmatch_t *restrict __pmatch, int __eflags)
    rc = regexec(&reg, match_text, matchs, match, 0);
    if (rc != 0) {
        char message[256];
        regerror(rc, &reg, message, 256);
        printf(">>> %s\n", message);
        return 1;
    }

    printf("%.*s, so = %d, eo = %d\n", match[0].rm_eo - match[0].rm_so, match_text + match[0].rm_so, match[0].rm_so, match[0].rm_eo);
    printf("%.*s, so = %d, eo = %d\n", match[1].rm_eo - match[1].rm_so, match_text + match[1].rm_so, match[1].rm_so, match[1].rm_eo);

    regfree(&reg);

    return 0;
}
// Output:
// 123, so = 0, eo = 3
// , so = -1, eo = -1


/****************************************************************
#include <sys/types.h>
#include <regex.h>

int regcomp(regex_t *preg, const char *regex, int cflags);

int regexec(const regex_t *preg, const char *string, size_t nmatch,
            regmatch_t pmatch[], int eflags);

size_t regerror(int errcode, const regex_t *preg, char *errbuf,
                size_t errbuf_size);

void regfree(regex_t *preg);
****************************************************************/